#! /bin/bash

set -ex

# Enable a console on ttyS0 so that we can log-in via vm-run.
#
echo 'GRUB_CMDLINE_LINUX="console=ttyS0,115200"' >>/etc/default/grub
update-grub

# We install all dependencies of the cockpit packages since we want
# them to not spontaneously change from one test run to the next when
# the distribution repository is updated.
#
COCKPIT_DEPS="\
cryptsetup \
docker.io \
libjson-glib-1.0-0 \
libpolkit-gobject-1-0 \
libpwquality-tools \
libssh-4 \
libteam-utils \
libvirt-daemon-system \
network-manager \
policykit-1 \
realmd \
selinux-basics \
storaged \
storaged-lvm2 \
thin-provisioning-tools \
xdg-utils \
"

# HACK - https://github.com/cockpit-project/cockpit/issues/3298
# We have a  explicit list of build dependencies here until we figure
# out how to let pbuilder install them.
#
COCKPIT_BUILD_DEPS="\
debhelper \
dh-autoreconf \
dh-systemd \
autoconf \
automake \
git \
intltool \
libssh-dev \
zlib1g-dev \
libkrb5-dev \
libxslt1-dev \
libglib2.0-dev \
libsystemd-dev \
libpolkit-agent-1-dev \
libjson-glib-dev \
libpam0g-dev \
systemd \
xsltproc \
xmlto \
docbook-xsl \
glib-networking \
openssh-client \
python \
"

# We also install the packages necessary to join a FreeIPA domain so
# that we don't have to go to the network during a test run.
#
IPA_CLIENT_PACKAGES="\
sssd-tools \
packagekit \
"

TEST_PACKAGES="\
curl \
gdb \
systemd-coredump \
virtinst \
"

useradd -m -U -c Administrator -G sudo -s /bin/bash admin
echo admin:foobar | chpasswd

# For storaged
echo "deb http://deb.debian.org/debian experimental main" >/etc/apt/sources.list.d/experimental.list
# FIXME: we need to enable jessie-backports until docker.io gets back into testing
echo "deb http://deb.debian.org/debian jessie-backports main" >/etc/apt/sources.list.d/jessie-backports.list
apt-get -y update

export DEBIAN_FRONTEND=noninteractive
apt-get -y install $TEST_PACKAGES $COCKPIT_DEPS $IPA_CLIENT_PACKAGES

# Prepare for building
#
apt-get -y install dpkg-dev pbuilder
echo "DISTRIBUTION=testing" > ~/.pbuilderrc
pbuilder --create
echo "apt-get -y install fakeroot $COCKPIT_BUILD_DEPS" >debian-8.install-build-deps
pbuilder --execute --save-after-exec debian-8.install-build-deps
# Disable build-dep installation for the real builds
echo PBUILDERSATISFYDEPENDSCMD=true >>~/.pbuilderrc

# Debian does not automatically start the default libvirt network
virsh net-autostart default

# HACK: docker falls over regularly, print its log if it does
# we also need to restart it in case aufs-dkms was installed after docker.io
# and thus docker.io auto-switches its backend
systemctl restart docker || journalctl -u docker

# docker images that we need for integration testing
/var/lib/testvm/docker-images.setup

# in case there are unnecessary packages
apt-get -y autoremove || true

# reduce image size
apt-get clean
/var/lib/testvm/zero-disk.setup

# Final tweaks

# Enable persistent journal
mkdir /var/log/journal

# Allow root login with password
sed -i 's/^[# ]*PermitRootLogin .*/PermitRootLogin yes/' /etc/ssh/sshd_config

# HACK: https://bugzilla.mindrot.org/show_bug.cgi?id=2512
# Disable the restarting of sshd when networking changes
ln -snf /bin/true /etc/network/if-up.d/openssh-server
